import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import "package:get/get.dart";

import 'components/status/toast.dart';
import 'config/global_config.dart';
import 'config/zh_cn_config.dart';
import 'controller/network/network_ctrl.dart';
import 'controller/notice_ctrl/notice_ctrl.dart';
import 'controller/platform/platform_ctrl.dart';
import 'router/index.dart';
import 'utils/screenutil/screenutil.dart';
import 'utils/tools/Tools.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化设计尺寸
  Screenutil.initDesignSize(640, 1136, density: 3);

  // 初始化相关服务
  await initServices();

  // 强制竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) => runApp(App()));
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    initServices();
    return GetMaterialApp(
      theme: ThemeData(
        primaryColor: GlobalConfig.theme,
        // 控制键盘高亮
        primaryColorBrightness: Brightness.dark,
        brightness: Brightness.light,
        accentColorBrightness: Brightness.light,
      ),
      debugShowCheckedModeBanner: false,
      getPages: AppComponents.router,
      initialRoute: AppComponents.INIT_PAGE,
      builder: EasyLoading.init(),
      supportedLocales: ZH_CN.locale,
      localizationsDelegates: ZH_CN.sinicizationConfig,
    );
  }
}

/// 预准备相关服务
Future<bool> initServices() async {
  // 初始化app信息
  await PlatformCtrl.getInstance();

  // 初始化工具类
  await Tools.getInstance();

  // 初始化Toast
  await Toast.getInstance();

  // 初始化网络监听
  await Get.put(NetworkCtrl.getInstance());

  // 初始化未读消息数量
  await Get.put(NoticeCtrl.getInstance());

  return true;
}
